<?php
    #-------------------------------------------------------------#
	# This file is part of the Kumomado - Virtual Window Platform #
	# for PHP. (c) 2009 Tsai, Min Che <tirme0812@gmail.com>       #
	# Please visit http://www.kumomado.org/ for more information. #
	#                                                             #
	# This software is licensed under the GNU License v3. Please  #
	# view the LICENSE file for the full text of this license.    #
    #-------------------------------------------------------------#

	/**
	 * @package libraries/logger/
	 * @author Tsai, Min Che
	 * @version	1.0
	 */
	class KumomadoLogger {
		public $error_types = array(
			E_USER_NOTICE => 'Notice',
			E_USER_WARNING => 'Warning',
			E_USER_ERROR => 'Error',
			E_NOTICE => 'Notice',
			E_WARNING => 'Warning',
			E_STRICT => 'Strict'
		);
		/**
		 * @var array
		 */
		private $queries = array();
		/**
		 * @var array
		 */
		private $errors = array();
		/**
		 * Construct
		 */
		private function __construct() {}
		/**
		 * @static
		 * @return object @link KumomadoLogger
		 */
		public static function getInstance() {
			static $KumomadoLogger;
			if ($KumomadoLogger === NULL) {
				$KumomadoLogger = new KumomadoLogger;
				error_reporting(E_ALL);
				set_error_handler(array($KumomadoLogger, 'handleError'));
			}
			return $KumomadoLogger;
		}
		/**
		 * Add the sql query history
		 * @param string $sql
		 * @param string $error[optional]
		 * @param int $error_no[optional]
		 */
		public function addQuery($sql, $error = null, $error_no = null) {
			$this->queries[] = array('sql' => $sql,
									'error' => $error,
									'error_no' => $error_no);
		}
		/**
		 *
		 * @param int $error_no
		 * @param string $error_string
		 * @param string $error_file
		 * @param int $error_line
		 * @param string $error_context
		 */
		public function handleError($error_no, $error_string, $error_file, $error_line, $error_context) {
			$this->errors[] = array('error_no' => $error_no,
									'error_string' => $this->sanitizePath($error_string),
									'error_file' => $this->sanitizePath($error_file),
									'error_line' => $error_line);
		}
		/**
		 * Print logger
		 */
		public function render() {
			foreach ($this->errors as &$error) {
				$error = $this->error_types[$error['error_no']].': '.$error['error_string'].' in file '.$error['error_file'].' line '.$error['error_line'];
			}
			return array(
				'queries' => $this->queries,
				'errors' => $this->errors
			);
			
//			echo '<table><tr><th>SQL Queries('.count($this->queries).')</th></tr>';
//			foreach ($this->queries as $query) {
//				echo '<tr><td>'.htmlentities($query['sql']);
//				echo isset($query['error']) ? '<div style="color: #F00;"><b>Error number:</b> '.$query['error_no'].'<br /><b>Error message:</b> '.$query['error'].'</span></div>' : '';
//				echo '</td></tr>';
//			}
//			echo '</table>';
//			echo '<table><tr><th>Error('.count($this->errors).')</th></tr>';
//			foreach ($this->errors as $error) {
//				echo '<tr><td>'.$error_types[$error['error_no']].': '.$error['error_string'].' in file '.$error['error_file'].' line '.$error['error_line'].'</td></tr>';
//			}
//			echo '</table>';
		}
		/**
		 *
		 * @return string
		 * @param string $path
		 */
		public function sanitizePath($path) {
			$path = str_replace(
				array('\\', KUMOMADO_ROOT_PATH, str_replace('\\', '/', realpath(KUMOMADO_ROOT_PATH))),
				array('/', '', ''),
				$path
			);
			return $path;
		}
		private function __clone() {}
	}
?>